<?php

namespace App\Http\Controllers\Asset;

use App\Http\Controllers\Controller;
use App\Http\Controllers\pagniateable;
use App\Models\Asset;
use App\Models\AssetBundle;
use App\Models\AssetBundleItem;
use App\Models\AssetStock;

class AssetBundleController extends Controller
{

    use pagniateable;

    public function list()
    {
        $query = AssetBundle::select("*");
        return $this->paginate($query);
    }

    public function add()
    {
        // check if name is already exist
        $isExists = AssetBundle::whereName(request()->get("name"))->count() > 0;
        if ($isExists) {
            $result = [
                "error" => true,
                "message" => "名称已经存在"
            ];
            return response()->json(compact(['result']));
        }
        $name = request()->get("name");
        $desc = request()->get("description");
        $desc = $desc == null ? "" : $desc;
        $asset = AssetBundle::create([
            "name" => $name,
            "description" => $desc
        ]);
        $result = $asset;
        return response()->json(compact(['result']));
    }

    public function del($id)
    {
        $bundle = AssetBundle::findOrFail($id);
        $bundle->delete();
        $result = 'ok';
        return response()->json(compact(['result']));
    }

    public function update($id)
    {
        $bundle = AssetBundle::findOrFail($id);
        $name = request()->get("name");
        $desc = request()->get("description");
        $desc = $desc == null ? "" : $desc;
        $bundle->name = $name;
        $bundle->description = $desc;
        $bundle->save();
        $assetBundle = $bundle;
        return response()->json(compact(['assetBundle']));
    }

    public function itemList($id)
    {
        $bundleItemsQuery = AssetBundleItem::whereAssetBundleId($id);
        return $this->paginate($bundleItemsQuery);
    }

    public function addItem($id)
    {
        $bundle = AssetBundle::findOrFail($id);
        $items = request()->get('data');
        foreach ($items as $item) {
            $model = $item['type'];
            $_id = $item['id'];
            $bundleItem = AssetBundleItem::create([
                "asset_bundle_id" => $id,
                "bundleable_id" => $_id,
                "bundleable_type" => $model
            ]);
            $bundle->items()->save($bundleItem);
        }
        $bundle->refresh()->with(['items']);
        $result = $bundle;
        return response()->json(compact(['result']));
    }

    public function delItem($id)
    {
        $bundle = AssetBundleItem::findOrFail($id);
        $bundle->delete();
        $result = 'ok';
        return response()->json(compact(['result']));
    }

    public function delItemByIds()
    {
        $ids = request()->get("ids");
        $items = AssetBundleItem::whereIn("id", $ids);
        $items->forceDelete();
        $result = 'ok';
        return response()->json(compact(['result']));
    }

    public function assetQuery()
    {
        $queryString = request()->get("query");
        $q = "%".$queryString. "%";
        // asset
        $assets = Asset::where("name", "like", $q)->select(["id", "name"])->get();
        $assetStocks = AssetStock::where("name", "like", $q)->select(["id", "name"])->get();
        $data = [
            Asset::class => $assets,
            AssetStock::class =>$assetStocks
        ];
        return response()->json(compact(['data']));
        // assetStock
    }
}
